package com.estate.struts.actions;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.estate.business.TenderCompanyService;
import com.estate.business.TenderService;
import com.estate.utils.ProjectConstants;
import com.estate.vo.Vo;
import com.estate.vo.impl.TenderCompanyVo;
import com.estate.vo.impl.TenderVo;

public class TenderCompanyAction extends EstateSupport {

	 private String id;
	 private String companyName;
		private String companyQuali;
		private String companyNum;
		private String agencyCode;
		private String mgrName;
		private String mgrPhone;
		private String contactPeople;
		private String contactPhone;
		private String regiDate;
	private String tenderId;
		
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private TenderCompanyService tenderCompanyService;
	private TenderService tenderService;
	
	private String directPage;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			TenderCompanyVo tenderCompany = this.getAddTenderCompany();
			if(tenderCompany != null){
				boolean result = this.getTenderCompanyService().addTenderCompany(tenderCompany);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> list = this.getTenderCompanyService().getList();
			this.getRequest().setAttribute("list", list);
//			this.getRequest().setAttribute("tenderId", this.getTenderId());
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allTenderCompany";
		}else if("detail".equals(this.getMethod())){
			Vo vo = this.getTenderCompanyService().getTenderCompany(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderCompany", vo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo vo = this.getTenderCompanyService().getTenderCompany(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderCompany", vo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			TenderCompanyVo tenderCompany = this.getUpdateTenderCompany();
			if(tenderCompany == null){
				page = "fail";
			}else{
				boolean result = this.getTenderCompanyService().updateTenderCompany(tenderCompany);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}
//		else if("post".equals(this.getMethod())){
//			this.getAnnouncementService().updatePost(Integer.parseInt(this.getId()));
//			page = this.SUCCESS;
//		}
		else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
//				System.out.println(this.getDelStr());
//				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getTenderCompanyService().deleteMultiTenderCompany(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
//				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					TenderCompanyVo tenderCompany = new TenderCompanyVo();
					tenderCompany.setId(Integer.parseInt(this.getId()));
					boolean result = this.getTenderCompanyService().deleteTenderCompany(tenderCompany);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else if("enterAddPage".equals(this.getMethod())){
			page="enterAddPage";
		}else if("getQualiList".equals(this.getMethod())){
			TenderVo tender = new TenderVo();
			tender.setId(Integer.parseInt(this.getTenderId()));
			List<TenderCompanyVo> tenderCompanyList = this.getTenderCompanyService().getTenderCompanyList(tender);
			this.getRequest().setAttribute("tenderCompanyList", tenderCompanyList);
			page = "enterPreQualiList";
		}else{
			page="fail";
		}
		
		if(this.getDirectPage() != null && !"".equals(this.getDirectPage())){
			page = this.getDirectPage();
		}
		return page;
	}
	private TenderCompanyVo getUpdateTenderCompany(){
		TenderCompanyVo tenderCompany = new TenderCompanyVo();
		
		int id = Integer.parseInt(this.getId());
		
		tenderCompany.setId(id);
		tenderCompany.setAgencyCode(this.getAgencyCode());
		tenderCompany.setCompanyName(this.getCompanyName());
		tenderCompany.setCompanyNum(this.getCompanyNum());
		tenderCompany.setCompanyQuali(this.getCompanyQuali());
		tenderCompany.setContactPeople(this.getContactPeople());
		tenderCompany.setContactPhone(this.getContactPhone());
		tenderCompany.setMgrName(this.getMgrName());
		tenderCompany.setMgrPhone(this.getMgrPhone());
		try {
			tenderCompany.setRegiDate(ProjectConstants.SIMPLE_DATE.parse(this.getRegiDate()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return tenderCompany;
	}
	private TenderCompanyVo getAddTenderCompany(){
		TenderCompanyVo tenderCompany = new TenderCompanyVo();
		
		tenderCompany.setAgencyCode(this.getAgencyCode());
		tenderCompany.setCompanyName(this.getCompanyName());
		tenderCompany.setCompanyNum(this.getCompanyNum());
		tenderCompany.setCompanyQuali(this.getCompanyQuali());
		tenderCompany.setContactPeople(this.getContactPeople());
		tenderCompany.setContactPhone(this.getContactPhone());
		tenderCompany.setMgrName(this.getMgrName());
		tenderCompany.setMgrPhone(this.getMgrPhone());
		tenderCompany.setPostDate(new Date());
		tenderCompany.setPostUser(this.getSessionUser());
		
		TenderVo tender = new TenderVo();
		tender.setId(Integer.parseInt(this.getTenderId()));
		tenderCompany.setTender(tender);
		try {
			tenderCompany.setRegiDate(ProjectConstants.SIMPLE_DATE.parse(this.getRegiDate()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
			
		return tenderCompany;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	public String getCompanyQuali() {
		return companyQuali;
	}
	public void setCompanyQuali(String companyQuali) {
		this.companyQuali = companyQuali;
	}
	public String getCompanyNum() {
		return companyNum;
	}
	public void setCompanyNum(String companyNum) {
		this.companyNum = companyNum;
	}
	public String getAgencyCode() {
		return agencyCode;
	}
	public void setAgencyCode(String agencyCode) {
		this.agencyCode = agencyCode;
	}
	public String getMgrName() {
		return mgrName;
	}
	public void setMgrName(String mgrName) {
		this.mgrName = mgrName;
	}
	public String getMgrPhone() {
		return mgrPhone;
	}
	public void setMgrPhone(String mgrPhone) {
		this.mgrPhone = mgrPhone;
	}
	public String getContactPeople() {
		return contactPeople;
	}
	public void setContactPeople(String contactPeople) {
		this.contactPeople = contactPeople;
	}
	public String getContactPhone() {
		return contactPhone;
	}
	public void setContactPhone(String contactPhone) {
		this.contactPhone = contactPhone;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public TenderCompanyService getTenderCompanyService() {
		return tenderCompanyService;
	}
	public void setTenderCompanyService(TenderCompanyService tenderCompanyService) {
		this.tenderCompanyService = tenderCompanyService;
	}
	public String getRegiDate() {
		return regiDate;
	}
	public void setRegiDate(String regiDate) {
		this.regiDate = regiDate;
	}
	public TenderService getTenderService() {
		return tenderService;
	}
	public void setTenderService(TenderService tenderService) {
		this.tenderService = tenderService;
	}
	public String getTenderId() {
		return tenderId;
	}
	public void setTenderId(String tenderId) {
		this.tenderId = tenderId;
	}
	public String getDirectPage() {
		return directPage;
	}
	public void setDirectPage(String directPage) {
		this.directPage = directPage;
	}
}
